package handler

import (
	"context"
	"fmt"
	orderpb "gitee.com/worklz/grpc-mtls-demo/server-order/proto/order"
	userpb "gitee.com/worklz/grpc-mtls-demo/server-user/proto/user"
)

type UserHandler struct {
	userpb.UnimplementedUserServiceServer // 关键：嵌入默认实现
	orderServiceClient                    orderpb.OrderServiceClient
}

var _ userpb.UserServiceServer = (*UserHandler)(nil)

func NewUserHandler(orderServiceClient orderpb.OrderServiceClient) *UserHandler {
	return &UserHandler{
		orderServiceClient: orderServiceClient,
	}
}

func (h *UserHandler) GetUser(ctx context.Context, req *userpb.GetUserRequest) (*userpb.UserResponse, error) {
	// 调用订单列表
	orderListRes, orderListErr := h.orderServiceClient.GetList(ctx, &orderpb.GetListRequest{
		Page:     1,
		PageSize: 10,
		UserId:   req.UserId,
	})
	if orderListErr != nil {
		return &userpb.UserResponse{
			Success: true,
			Message: fmt.Sprintf("获取用户订单列表失败！%v", orderListErr),
		}, nil
	}
	return &userpb.UserResponse{
		Success: true,
		Message: fmt.Sprintf("获取用户详情成功！user_id=%d 用户订单数据：%s", req.UserId, orderListRes.Message),
	}, nil
}
